package com.ryskoo.catering.productcate.service;

import com.ryskoo.catering.productcate.domain.ProductCategoryPO;
import com.ryskoo.catering.productcate.repository.IProductCategoryRepository;
import com.ryskoo.common.constant.EurekaServiceName;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
@Transactional
public class ProductCategoryServiceImpl extends BasicServiceImpl<ProductCategoryPO> implements IProductCategoryService {
    private static final Logger logger = LogManager.getLogger(EurekaServiceName.CATERING_SERVICE);

    @Autowired
    private IProductCategoryRepository productCategoryRepository;

    @PostConstruct
    public void init() {
        this.setDao(productCategoryRepository);
    }

    @Override
    public List<ProductCategoryPO> getListByVendorId(String vendorId) {
        return productCategoryRepository.getListByVendorId(vendorId);
    }

    @Override
    public List<String> getUuidsByNameAndVendorId(String name, String vendorId) {
        return productCategoryRepository.getUuidsByNameAndVendorId(name, vendorId);
    }

    @Override
    public List<String> getUuidsByNameAndVendorIdForUpdate(String name, String vendorId, String uuid) {
        return productCategoryRepository.getUuidsByNameAndVendorIdForUpdate(name, vendorId, uuid);
    }

    @Override
    public void changeStatus(List<String> uuids, boolean status) {
        productCategoryRepository.changeStatus(uuids, status);
    }

    @Override
    public ProductCategoryPO up(String uuid) {
        ProductCategoryPO cate = productCategoryRepository.getByUuid(uuid);
        int position = cate.getPosition();
        // 如果position==0，那么则不用上移
        if (position != 0) {
            QueryMessageDTO qm = new QueryMessageDTO();
            qm.addCondition("position", "EQ", position - 1);
            qm.addCondition("vendorUuid", "EQ", cate.getVendorUuid());
            List<ProductCategoryPO> list = productCategoryRepository.queryByCondition(qm, false);
            for (ProductCategoryPO po : list) {
                int position1 = po.getPosition();
                po.setPosition(position1 + 1);
                productCategoryRepository.update(po);
            }
            cate.setPosition(position - 1);
            productCategoryRepository.update(cate);
        }
        return cate;
    }

    @Override
    public ProductCategoryPO down(String uuid) {
        ProductCategoryPO cate = productCategoryRepository.getByUuid(uuid);
        int position = cate.getPosition();
        int maxPosition = productCategoryRepository.getMaxPosition(cate.getVendorUuid());
        if (position < maxPosition) {
            QueryMessageDTO qm = new QueryMessageDTO();
            qm.addCondition("position", "EQ", position + 1);
            qm.addCondition("vendorUuid", "EQ", cate.getVendorUuid());
            List<ProductCategoryPO> list = productCategoryRepository.queryByCondition(qm, false);
            for (ProductCategoryPO po : list) {
                int position1 = po.getPosition();
                po.setPosition(position1 - 1);
                productCategoryRepository.update(po);
            }
            cate.setPosition(position + 1);
            productCategoryRepository.update(cate);
        }
        return cate;
    }

    @Override
    public void deletes(List<String> uuids) {
        super.deletes(uuids);
        // 删除ES里的商品信息
        deleteCategoryFromES(uuids);
    }

    @Override
    public int getMaxPosition(String vendorUuid) {
        return productCategoryRepository.getMaxPosition(vendorUuid);
    }

    @Override
    public List<ProductCategoryPO> queryFromES(QueryMessageDTO qm) {
        return productCategoryRepository.queryFromES(qm, qm.isByPage());
    }

    @Override
    public int getCountFromES(QueryMessageDTO qm) {
        return productCategoryRepository.getCountFromES(qm);
    }

    /**
     * 同步商品分类信息到ES
     *
     * @param qm
     */
    @Override
    public void syncCategoryToES(QueryMessageDTO qm) {
        logger.info("***** 同步商品分类到ES 开始 *****");
        List<ProductCategoryPO> list = queryByCondition(qm);
        if (null != list && list.size() > 0) {
            productCategoryRepository.syncToElastic(list);
            logger.info("同步 {} 条商品分类......", list.size());
        }
        logger.info("***** 同步商品分类到ES 完成 *****");
    }

    /**
     * 删除ES里商品分类信息
     *
     * @param idList
     */
    @Override
    public void deleteCategoryFromES(List<String> idList) {
        if (null != idList && idList.size() > 0) {
            productCategoryRepository.deleteProductFromES(idList);
        }
    }
}
